﻿<?php
	session_start();
	$forwardPage = "login.php";
	if(!isset($_SESSION['nit'])){
		header("Location: " . $forwardPage);
		exit();
	}
	$cedula = $_GET["cedula"];
	
	include "config.php";
	
	
	
	$valores = array(':nit_empresa' => $_SESSION['nit'],
						':cedula' => $cedula
		);
	
	$query1 = "SELECT id FROM reporte_ausentismo " . 
			"WHERE nit_empresa = :nit_empresa AND cedula = :cedula AND (fecha_registro BETWEEN DATE(CONCAT(YEAR(NOW()), '-01-01')) AND DATE(CONCAT(YEAR(NOW()), '-12-31')))";
	
	$query2 = "SELECT (MAX(cantidad_incapacidades) + 1) AS cantidad_incapacidades FROM reporte_ausentismo " . 
			"WHERE nit_empresa = :nit_empresa AND cedula = :cedula";
	
	try{
		$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $password, $options);
		$stmt = $dbh -> prepare($query1);
		$stmt -> execute($valores);
		/* echo "\nPDOStatement::errorInfo():\n";
		$arr = $stmt->errorInfo();
		print_r($arr); */
		$result = "{";
		$numFilas = $stmt -> rowCount();
		if ($numFilas >= 1){
			$result .= '"clasifAno": "Incidencia"';
		}
		else{
			$result .= '"clasifAno": "Prevalencia"';
		}
		
		$stmt = $dbh -> prepare($query2);
		$stmt -> execute($valores);
		/* echo "\nPDOStatement::errorInfo():\n";
		$arr = $stmt->errorInfo();
		print_r($arr); */
		$numFilas = $stmt -> rowCount();
		//echo $numFilas;
		$reg = $stmt -> fetch();
		if ($reg['cantidad_incapacidades'] != NULL){
			$result .= ', "cantIncapacidades": "' . $reg['cantidad_incapacidades'] . '"';
		}
		else{
			$result .= ', "cantIncapacidades": "1"';
		}
		
		$result .= "}";
		echo $result;
		
		$dbh = null;
	}
	catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
?>